home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / lisp-mode.el < prev    next >
Encoding:
Text File  |  1995-07-10  |  30.9 KB  |  816 lines

  1. ;;; lisp-mode.el --- Lisp mode, and its idiosyncratic commands.
  2.  
  3. ;; Copyright (C) 1985, 1993, 1994 Free Software Foundation, Inc.
  4. ;; Copyright (C) 1995 Tinker Systems
  5.  
  6. ;; Maintainer: FSF
  7. ;; Keywords: lisp, languages
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Synched up with: FSF 19.28.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; The base major mode for editing Lisp code (used also for Emacs Lisp).
  30. ;; This mode is documented in the Emacs manual
  31.  
  32. ;;; Code:
  33.  
  34. (defvar lisp-mode-syntax-table nil "")
  35. (defvar emacs-lisp-mode-syntax-table nil "")
  36. (defvar lisp-mode-abbrev-table nil "")
  37.  
  38. (defvar lisp-interaction-popup-menu
  39.   (purecopy '("Lisp-interaction menu"
  40.           ["Evaluate last expression" eval-last-sexp      t]
  41.           ["Evaluate entire buffer"   eval-current-buffer t]
  42.           "---"
  43.           ["Evaluate this defun"      eval-defun          t]
  44.           ["Debug this defun"         edebug-defun        t]
  45.           "---"
  46.           ["Trace a function"   trace-function-background t]
  47.           ["Untrace all functions"    untrace-all (fboundp 'untrace-all)]
  48.           "---"
  49.           ["Debug on error" (setq debug-on-error (not debug-on-error))
  50.            :style toggle :selected debug-on-error]
  51.           ["Debug on quit" (setq debug-on-quit (not debug-on-quit))
  52.            :style toggle :selected debug-on-quit]
  53.           )))
  54.  
  55. (defvar emacs-lisp-popup-menu
  56.   (purecopy
  57.    (nconc
  58.     '("Emacs-Lisp menu"
  59.       ["Byte-compile file" (progn (save-buffer)
  60.                   (byte-compile-file buffer-file-name)) t]
  61.       "---")
  62.     (cdr lisp-interaction-popup-menu))))
  63.  
  64. (if (not emacs-lisp-mode-syntax-table)
  65.     (let ((i 0))
  66.       (setq emacs-lisp-mode-syntax-table (make-syntax-table))
  67.       (while (< i ?0)
  68.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  69.     (setq i (1+ i)))
  70.       (setq i (1+ ?9))
  71.       (while (< i ?A)
  72.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  73.     (setq i (1+ i)))
  74.       (setq i (1+ ?Z))
  75.       (while (< i ?a)
  76.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  77.     (setq i (1+ i)))
  78.       (setq i (1+ ?z))
  79.       (while (< i 128)
  80.     (modify-syntax-entry i "_   " emacs-lisp-mode-syntax-table)
  81.     (setq i (1+ i)))
  82.       (modify-syntax-entry ?  "    " emacs-lisp-mode-syntax-table)
  83.       (modify-syntax-entry ?\t "    " emacs-lisp-mode-syntax-table)
  84.       (modify-syntax-entry ?\n ">   " emacs-lisp-mode-syntax-table)
  85.       ;; Give CR the same syntax as newline, for selective-display.
  86.       (modify-syntax-entry ?\^m ">   " emacs-lisp-mode-syntax-table)
  87.       ;; WRONG, ^L does not terminate a comment.  Treat it as whitespace? -jwz
  88.       ;;(modify-syntax-entry ?\f ">   " emacs-lisp-mode-syntax-table)
  89.       (modify-syntax-entry ?\f "    " emacs-lisp-mode-syntax-table)
  90.       (modify-syntax-entry ?\; "<   " emacs-lisp-mode-syntax-table)
  91.       (modify-syntax-entry ?` "'   " emacs-lisp-mode-syntax-table)
  92.       (modify-syntax-entry ?' "'   " emacs-lisp-mode-syntax-table)
  93.       (modify-syntax-entry ?, "'   " emacs-lisp-mode-syntax-table)
  94.       ;; Used to be singlequote; changed for flonums.
  95.       (modify-syntax-entry ?. "_   " emacs-lisp-mode-syntax-table)
  96.       (modify-syntax-entry ?# "'   " emacs-lisp-mode-syntax-table)
  97.       (modify-syntax-entry ?\" "\"    " emacs-lisp-mode-syntax-table)
  98.       (modify-syntax-entry ?\\ "\\   " emacs-lisp-mode-syntax-table)
  99.       (modify-syntax-entry ?\( "()  " emacs-lisp-mode-syntax-table)
  100.       (modify-syntax-entry ?\) ")(  " emacs-lisp-mode-syntax-table)
  101.       (modify-syntax-entry ?\[ "(]  " emacs-lisp-mode-syntax-table)
  102.       (modify-syntax-entry ?\] ")[  " emacs-lisp-mode-syntax-table)))
  103.  
  104. (if (not lisp-mode-syntax-table)
  105.     (progn (setq lisp-mode-syntax-table
  106.          (copy-syntax-table emacs-lisp-mode-syntax-table))
  107.        (modify-syntax-entry ?\[ "_   " lisp-mode-syntax-table)
  108.        (modify-syntax-entry ?\] "_   " lisp-mode-syntax-table)
  109.            ;;
  110.            ;; If emacs was compiled with NEW_SYNTAX, then do
  111.            ;;  CL's #| |# block comments.
  112.            (if (= 8 (length (parse-partial-sexp (point) (point))))
  113.                (progn
  114.                  (modify-syntax-entry ?#  "' 58" lisp-mode-syntax-table)
  115.                  (modify-syntax-entry ?|  ". 67" lisp-mode-syntax-table))
  116.                ;; else, old style
  117.                (modify-syntax-entry ?\| "\"   " lisp-mode-syntax-table))))
  118.  
  119. (define-abbrev-table 'lisp-mode-abbrev-table ())
  120.  
  121. (defun lisp-mode-variables (lisp-syntax)
  122.   (if lisp-syntax
  123.       (set-syntax-table lisp-mode-syntax-table))
  124.   (setq local-abbrev-table lisp-mode-abbrev-table)
  125.   (make-local-variable 'paragraph-start)
  126.   (setq paragraph-start (concat "^$\\|" page-delimiter))
  127.   (make-local-variable 'paragraph-separate)
  128.   (setq paragraph-separate paragraph-start)
  129.   (make-local-variable 'paragraph-ignore-fill-prefix)
  130.   (setq paragraph-ignore-fill-prefix t)
  131.   (make-local-variable 'indent-line-function)
  132.   (setq indent-line-function 'lisp-indent-line)
  133.   (make-local-variable 'indent-region-function)
  134.   (setq indent-region-function 'lisp-indent-region)
  135.   (make-local-variable 'parse-sexp-ignore-comments)
  136.   (setq parse-sexp-ignore-comments t)
  137.   (make-local-variable 'outline-regexp)
  138.   (setq outline-regexp ";;; \\|(....")
  139.   (set (make-local-variable 'comment-start) ";")
  140.   (set (make-local-variable 'block-comment-start) ";;")
  141.   (make-local-variable 'comment-start-skip)
  142.   (setq comment-start-skip ";+[ \t]*")
  143.   (make-local-variable 'comment-column)
  144.   (setq comment-column 40)
  145.   (make-local-variable 'comment-indent-function)
  146.   (setq comment-indent-function 'lisp-comment-indent)
  147.   (set (make-local-variable 'dabbrev-case-fold-search) nil)
  148.   (set (make-local-variable 'dabbrev-case-replace) nil)
  149.   )
  150.  
  151.  
  152. (defvar shared-lisp-mode-map ()
  153.   "Keymap for commands shared by all sorts of Lisp modes.")
  154.  
  155. (if shared-lisp-mode-map
  156.     ()
  157.    (setq shared-lisp-mode-map (make-sparse-keymap))
  158.    (set-keymap-name shared-lisp-mode-map 'shared-lisp-mode-map)
  159.    (define-key shared-lisp-mode-map "\e\C-q" 'indent-sexp)
  160.    (define-key shared-lisp-mode-map "\M-q" 'lisp-fill-paragraph)
  161.    (define-key shared-lisp-mode-map "\177" 'backward-delete-char-untabify)
  162.    (define-key shared-lisp-mode-map "\M-;" 'lisp-indent-for-comment)
  163.    (define-key shared-lisp-mode-map "\t" 'lisp-indent-line))
  164.  
  165. (defvar emacs-lisp-mode-map ()
  166.   "Keymap for Emacs Lisp mode.
  167. All commands in shared-lisp-mode-map are inherited by this map.")
  168.  
  169. (if emacs-lisp-mode-map
  170.     ()
  171.   (setq emacs-lisp-mode-map (make-sparse-keymap))
  172.   (set-keymap-name emacs-lisp-mode-map 'emacs-lisp-mode-map)
  173.   (set-keymap-parents emacs-lisp-mode-map (list shared-lisp-mode-map))
  174.   (define-key emacs-lisp-mode-map "\e\t" 'lisp-complete-symbol)
  175.   (define-key emacs-lisp-mode-map "\e\C-x" 'eval-defun))
  176.  
  177. (defun emacs-lisp-mode ()
  178.   "Major mode for editing Lisp code to run in Emacs.
  179. Commands:
  180. Delete converts tabs to spaces as it moves back.
  181. Blank lines separate paragraphs.  Semicolons start comments.
  182. \\{emacs-lisp-mode-map}
  183. Entry to this mode calls the value of `emacs-lisp-mode-hook'
  184. if that value is non-nil."
  185.   (interactive)
  186.   (kill-all-local-variables)
  187.   (use-local-map emacs-lisp-mode-map)
  188.   (set-syntax-table emacs-lisp-mode-syntax-table)
  189.   (setq major-mode 'emacs-lisp-mode
  190.     mode-popup-menu emacs-lisp-popup-menu
  191.     mode-name "Emacs-Lisp")
  192.   (lisp-mode-variables nil)
  193.   (run-hooks 'emacs-lisp-mode-hook))
  194.  
  195. (defvar lisp-mode-map ()
  196.   "Keymap for ordinary Lisp mode.
  197. All commands in `shared-lisp-mode-map' are inherited by this map.")
  198.  
  199. (if lisp-mode-map
  200.     ()
  201.   (setq lisp-mode-map (make-sparse-keymap))
  202.   (set-keymap-name lisp-mode-map 'lisp-mode-map)
  203.   (set-keymap-parents lisp-mode-map (list shared-lisp-mode-map))
  204.   (define-key lisp-mode-map "\e\C-x" 'lisp-send-defun)
  205.   ;; gag, no.  use ilisp.  -jwz
  206. ;;  (define-key lisp-mode-map "\C-c\C-z" 'run-lisp)
  207.   )
  208.  
  209. (defun lisp-mode ()
  210.   "Major mode for editing Lisp code for Lisps other than GNU Emacs Lisp.
  211. Commands:
  212. Delete converts tabs to spaces as it moves back.
  213. Blank lines separate paragraphs.  Semicolons start comments.
  214. \\{lisp-mode-map}
  215. Note that `run-lisp' may be used either to start an inferior Lisp job
  216. or to switch back to an existing one.
  217.  
  218. Entry to this mode calls the value of `lisp-mode-hook'
  219. if that value is non-nil."
  220.   (interactive)
  221.   (kill-all-local-variables)
  222.   (use-local-map lisp-mode-map)
  223.   (setq major-mode 'lisp-mode)
  224.   (setq mode-name "Lisp")
  225.   (lisp-mode-variables t)
  226.   (set-syntax-table lisp-mode-syntax-table)
  227.   (run-hooks 'lisp-mode-hook))
  228.  
  229. ;; This will do unless shell.el is loaded.
  230. (defun lisp-send-defun ()
  231.   "Send the current defun to the Lisp process made by \\[run-lisp]."
  232.   (interactive)
  233.   (error "Process lisp does not exist"))
  234.  
  235. ;; XEmacs change: emacs-lisp-mode-map is a more appropriate parent.
  236. (defvar lisp-interaction-mode-map nil
  237.   "Keymap for Lisp Interaction moe.
  238. All commands in `emacs-lisp-mode-map' are inherited by this map.")
  239.  
  240. (if lisp-interaction-mode-map
  241.     ()
  242.   (setq lisp-interaction-mode-map (make-sparse-keymap))
  243.   (set-keymap-name lisp-interaction-mode-map 'lisp-interaction-mode-map)
  244.   (set-keymap-parents lisp-interaction-mode-map (list emacs-lisp-mode-map))
  245.   (define-key lisp-interaction-mode-map "\e\C-x" 'eval-defun)
  246.   (define-key lisp-interaction-mode-map "\e\t" 'lisp-complete-symbol)
  247.   (define-key lisp-interaction-mode-map "\n" 'eval-print-last-sexp))
  248.  
  249. (defun lisp-interaction-mode ()
  250.   "Major mode for typing and evaluating Lisp forms.
  251. Like Lisp mode except that \\[eval-print-last-sexp] evals the Lisp expression
  252. before point, and prints its value into the buffer, advancing point.
  253.  
  254. Commands:
  255. Delete converts tabs to spaces as it moves back.
  256. Paragraphs are separated only by blank lines.  Semicolons start comments.
  257. \\{lisp-interaction-mode-map}
  258. Entry to this mode calls the value of `lisp-interaction-mode-hook'
  259. if that value is non-nil."
  260.   (interactive)
  261.   (kill-all-local-variables)
  262.   (use-local-map lisp-interaction-mode-map)
  263.   (setq major-mode 'lisp-interaction-mode
  264.     mode-popup-menu lisp-interaction-popup-menu
  265.     mode-name "Lisp Interaction")
  266.   (set-syntax-table emacs-lisp-mode-syntax-table)
  267.   (lisp-mode-variables nil)
  268.   (run-hooks 'lisp-interaction-mode-hook))
  269.  
  270. (defun eval-print-last-sexp ()
  271.   "Evaluate sexp before point; print value into current buffer."
  272.   (interactive)
  273.   (let ((standard-output (current-buffer)))
  274.     (terpri)
  275.     (eval-last-sexp t)
  276.     (terpri)))
  277.  
  278. (defun eval-interactive (expr)
  279.   "Like `eval' except that it transforms defvars to defconsts."
  280.   ;; by Stig@hackvan.com
  281.   ;; #### - My preference would be for this function to be used instead of
  282.   ;; `eval' in both `eval-defun' and `eval-last-sexp', but that would take
  283.   ;; some consensus.  For now it's used by `mouse-eval-last-sexp'.
  284.   ;; #### - Consensus achieved.  RMS will be going with this too.  
  285.   (if (and (consp expr)
  286.        (eq (car expr) 'defvar)
  287.        (> (length expr) 2))
  288.       (progn (eval (cons 'defconst (cdr expr)))
  289.          (message "defvar treated as defconst")
  290.          (sit-for 1)
  291.          (message ""))
  292.     (eval expr)))
  293.  
  294. (defun eval-last-sexp (eval-last-sexp-arg-internal) ;dynamic scoping wonderment
  295.   "Evaluate sexp before point; print value in minibuffer.
  296. With argument, print output into current buffer."
  297.   (interactive "P")
  298.   (let ((standard-output (if eval-last-sexp-arg-internal (current-buffer) t))
  299.     (opoint (point)))
  300.     (prin1 (let ((stab (syntax-table)))
  301.          (eval-interactive (unwind-protect
  302.                    (save-excursion
  303.                      (set-syntax-table emacs-lisp-mode-syntax-table)
  304.                      (forward-sexp -1)
  305.                      (save-restriction
  306.                        (narrow-to-region (point-min) opoint)
  307.                        (read (current-buffer))))
  308.                  (set-syntax-table stab)))))))
  309.  
  310. (defun eval-defun (eval-defun-arg-internal) ;dynamic scoping wonderment
  311.   "Evaluate defun that point is in or before.
  312. Print value in minibuffer.
  313. With argument, insert value in current buffer after the defun."
  314.   (interactive "P")
  315.   (let ((standard-output (if eval-defun-arg-internal (current-buffer) t)))
  316.     (prin1 (eval-interactive (save-excursion
  317.                    (end-of-defun)
  318.                    (beginning-of-defun)
  319.                    (read (current-buffer)))))))
  320.  
  321. (defun lisp-comment-indent ()
  322.   (if (looking-at "\\s<\\s<\\s<")
  323.       (current-column)
  324.     (if (looking-at "\\s<\\s<")
  325.     (let ((tem (calculate-lisp-indent)))
  326.       (if (listp tem) (car tem) tem))
  327.       (skip-chars-backward " \t")
  328.       (max (if (bolp) 0 (1+ (current-column)))
  329.        comment-column))))
  330.  
  331. (defun lisp-indent-for-comment ()
  332.   "Indent this line's comment appropriately, or insert an empty comment.
  333. If adding a new comment on a blank line, use `block-comment-start' instead
  334. of `comment-start' to open the comment."
  335.   ;; by Stig@hackvan.com
  336.   ;; #### - This functionality, the recognition of block-comment-{start,end},
  337.   ;; will perhaps be standardized across modes and move to indent-for-comment.
  338.   (interactive)
  339.   (if (and block-comment-start
  340.        (save-excursion (beginning-of-line) (looking-at "^[ \t]*$")))
  341.       (insert block-comment-start))
  342.   (indent-for-comment))
  343.  
  344. (defconst lisp-indent-offset nil "")
  345. (defconst lisp-indent-function 'lisp-indent-function "")
  346.  
  347. (defun lisp-indent-line (&optional whole-exp)
  348.   "Indent current line as Lisp code.
  349. With argument, indent any additional lines of the same expression
  350. rigidly along with this one."
  351.   (interactive "P")
  352.   (let ((indent (calculate-lisp-indent)) shift-amt beg end
  353.     (pos (- (point-max) (point))))
  354.     (beginning-of-line)
  355.     (setq beg (point))
  356.     (skip-chars-forward " \t")
  357.     (if (looking-at "\\s<\\s<\\s<")
  358.     ;; Don't alter indentation of a ;;; comment line.
  359.     (goto-char (- (point-max) pos))
  360.       (if (and (looking-at "\\s<") (not (looking-at "\\s<\\s<")))
  361.       ;; Single-semicolon comment lines should be indented
  362.       ;; as comment lines, not as code.
  363.       (progn (indent-for-comment) (forward-char -1))
  364.     (if (listp indent) (setq indent (car indent)))
  365.     (setq shift-amt (- indent (current-column)))
  366.     (if (zerop shift-amt)
  367.         nil
  368.       (delete-region beg (point))
  369.       (indent-to indent)))
  370.       ;; If initial point was within line's indentation,
  371.       ;; position after the indentation.  Else stay at same point in text.
  372.       (if (> (- (point-max) pos) (point))
  373.       (goto-char (- (point-max) pos)))
  374.       ;; If desired, shift remaining lines of expression the same amount.
  375.       (and whole-exp (not (zerop shift-amt))
  376.        (save-excursion
  377.          (goto-char beg)
  378.          (forward-sexp 1)
  379.          (setq end (point))
  380.          (goto-char beg)
  381.          (forward-line 1)
  382.          (setq beg (point))
  383.          (> end beg))
  384.        (indent-code-rigidly beg end shift-amt)))))
  385.  
  386. (defun calculate-lisp-indent (&optional parse-start)
  387.   "Return appropriate indentation for current line as Lisp code.
  388. In usual case returns an integer: the column to indent to.
  389. Can instead return a list, whose car is the column to indent to.
  390. This means that following lines at the same level of indentation
  391. should not necessarily be indented the same way.
  392. The second element of the list is the buffer position
  393. of the start of the containing expression."
  394.   (save-excursion
  395.     (beginning-of-line)
  396.     (let ((indent-point (point))
  397.           state ;;paren-depth
  398.           ;; setting this to a number inhibits calling hook
  399.           (desired-indent nil)
  400.           (retry t)
  401.           last-sexp containing-sexp)
  402.       (if parse-start
  403.           (goto-char parse-start)
  404.           (beginning-of-defun))
  405.       ;; Find outermost containing sexp
  406.       (while (< (point) indent-point)
  407.         (setq state (parse-partial-sexp (point) indent-point 0)))
  408.       ;; Find innermost containing sexp
  409.       (while (and retry
  410.           state
  411.                   (> ;;(setq paren-depth (elt state 0))
  412.              (elt state 0)
  413.              0))
  414.         (setq retry nil)
  415.         (setq last-sexp (elt state 2))
  416.         (setq containing-sexp (elt state 1))
  417.         ;; Position following last unclosed open.
  418.         (goto-char (1+ containing-sexp))
  419.         ;; Is there a complete sexp since then?
  420.         (if (and last-sexp (> last-sexp (point)))
  421.             ;; Yes, but is there a containing sexp after that?
  422.             (let ((peek (parse-partial-sexp last-sexp indent-point 0)))
  423.               (if (setq retry (car (cdr peek))) (setq state peek)))))
  424.       (if retry
  425.           nil
  426.         ;; Innermost containing sexp found
  427.         (goto-char (1+ containing-sexp))
  428.         (if (not last-sexp)
  429.         ;; indent-point immediately follows open paren.
  430.         ;; Don't call hook.
  431.             (setq desired-indent (current-column))
  432.       ;; Find the start of first element of containing sexp.
  433.       (parse-partial-sexp (point) last-sexp 0 t)
  434.       (cond ((looking-at "\\s(")
  435.          ;; First element of containing sexp is a list.
  436.          ;; Indent under that list.
  437.          )
  438.         ((> (save-excursion (forward-line 1) (point))
  439.             last-sexp)
  440.          ;; This is the first line to start within the containing sexp.
  441.          ;; It's almost certainly a function call.
  442.          (if (= (point) last-sexp)
  443.              ;; Containing sexp has nothing before this line
  444.              ;; except the first element.  Indent under that element.
  445.              nil
  446.            ;; Skip the first element, find start of second (the first
  447.            ;; argument of the function call) and indent under.
  448.            (progn (forward-sexp 1)
  449.               (parse-partial-sexp (point) last-sexp 0 t)))
  450.          (backward-prefix-chars))
  451.         (t
  452.          ;; Indent beneath first sexp on same line as last-sexp.
  453.          ;; Again, it's almost certainly a function call.
  454.          (goto-char last-sexp)
  455.          (beginning-of-line)
  456.          (parse-partial-sexp (point) last-sexp 0 t)
  457.          (backward-prefix-chars)))))
  458.       ;; Point is at the point to indent under unless we are inside a string.
  459.       ;; Call indentation hook except when overridden by lisp-indent-offset
  460.       ;; or if the desired indentation has already been computed.
  461.       (let ((normal-indent (current-column)))
  462.         (cond ((elt state 3)
  463.                ;; Inside a string, don't change indentation.
  464.                (goto-char indent-point)
  465.                (skip-chars-forward " \t")
  466.                (current-column))
  467.               (desired-indent)
  468.               ((and (boundp 'lisp-indent-function)
  469.                     lisp-indent-function
  470.                     (not retry))
  471.                (or (funcall lisp-indent-function indent-point state)
  472.                    normal-indent))
  473.               ;; lisp-indent-offset shouldn't override lisp-indent-function !
  474.               ((and (integerp lisp-indent-offset) containing-sexp)
  475.                ;; Indent by constant offset
  476.                (goto-char containing-sexp)
  477.                (+ normal-indent lisp-indent-offset))
  478.               (t
  479.                normal-indent))))))
  480.  
  481. (defun lisp-indent-function (indent-point state)
  482.   ;; free reference to `last-sexp' in #'calculate-lisp-indent
  483.   (let ((normal-indent (current-column)))
  484.     (goto-char (1+ (elt state 1)))
  485.     (parse-partial-sexp (point) last-sexp 0 t)
  486.     (if (and (elt state 2)
  487.              (not (looking-at "\\sw\\|\\s_")))
  488.         ;; car of form doesn't seem to be a a symbol
  489.         (progn
  490.           (if (not (> (save-excursion (forward-line 1) (point))
  491.                       last-sexp))
  492.               (progn (goto-char last-sexp)
  493.                      (beginning-of-line)
  494.                      (parse-partial-sexp (point) last-sexp 0 t)))
  495.           ;; Indent under the list or under the first sexp on the
  496.           ;; same line as last-sexp.  Note that first thing on that
  497.           ;; line has to be complete sexp since we are inside the
  498.           ;; innermost containing sexp.
  499.           (backward-prefix-chars)
  500.           (current-column))
  501.       (let ((function (buffer-substring (point)
  502.                     (progn (forward-sexp 1) (point))))
  503.         method)
  504.     (setq method (or (get (intern-soft function) 'lisp-indent-function)
  505.                          (get (intern-soft function) 'lisp-indent-hook)))
  506.     (cond ((or (eq method 'defun)
  507.            (and (null method)
  508.             (> (length function) 3)
  509.             (string-match "\\`def" function)))
  510.            (lisp-indent-defform state indent-point))
  511.           ((integerp method)
  512.            (lisp-indent-specform method state
  513.                      indent-point normal-indent))
  514.           (method
  515.         (funcall method state indent-point)))))))
  516.  
  517. (defconst lisp-body-indent 2
  518.   "Number of columns to indent the second line of a `(def...)' form.")
  519.  
  520. (defun lisp-indent-specform (count state indent-point normal-indent)
  521.   (let ((containing-form-start (elt state 1))
  522.         (i count)
  523.         body-indent containing-form-column)
  524.     ;; Move to the start of containing form, calculate indentation
  525.     ;; to use for non-distinguished forms (> count), and move past the
  526.     ;; function symbol.  lisp-indent-function guarantees that there is at
  527.     ;; least one word or symbol character following open paren of containing
  528.     ;; form.
  529.     (goto-char containing-form-start)
  530.     (setq containing-form-column (current-column))
  531.     (setq body-indent (+ lisp-body-indent containing-form-column))
  532.     (forward-char 1)
  533.     (forward-sexp 1)
  534.     ;; Now find the start of the last form.
  535.     (parse-partial-sexp (point) indent-point 1 t)
  536.     (while (and (< (point) indent-point)
  537.                 (condition-case ()
  538.                     (progn
  539.                       (setq count (1- count))
  540.                       (forward-sexp 1)
  541.                       (parse-partial-sexp (point) indent-point 1 t))
  542.                   (error nil))))
  543.     ;; Point is sitting on first character of last (or count) sexp.
  544.     (if (> count 0)
  545.         ;; A distinguished form.  If it is the first or second form use double
  546.         ;; lisp-body-indent, else normal indent.  With lisp-body-indent bound
  547.         ;; to 2 (the default), this just happens to work the same with if as
  548.         ;; the older code, but it makes unwind-protect, condition-case,
  549.         ;; with-output-to-temp-buffer, et. al. much more tasteful.  The older,
  550.         ;; less hacked, behavior can be obtained by replacing below with
  551.         ;; (list normal-indent containing-form-start).
  552.         (if (<= (- i count) 1)
  553.             (list (+ containing-form-column (* 2 lisp-body-indent))
  554.                   containing-form-start)
  555.             (list normal-indent containing-form-start))
  556.       ;; A non-distinguished form.  Use body-indent if there are no
  557.       ;; distinguished forms and this is the first undistinguished form,
  558.       ;; or if this is the first undistinguished form and the preceding
  559.       ;; distinguished form has indentation at least as great as body-indent.
  560.       (if (or (and (= i 0) (= count 0))
  561.               (and (= count 0) (<= body-indent normal-indent)))
  562.           body-indent
  563.           normal-indent))))
  564.  
  565. (defun lisp-indent-defform (state indent-point)
  566.   (goto-char (car (cdr state)))
  567.   (forward-line 1)
  568.   (if (> (point) (car (cdr (cdr state))))
  569.       (progn
  570.     (goto-char (car (cdr state)))
  571.     (+ lisp-body-indent (current-column)))))
  572.  
  573.  
  574. ;; (put 'progn 'lisp-indent-function 0), say, causes progn to be indented
  575. ;; like defun if the first form is placed on the next line, otherwise
  576. ;; it is indented like any other form (i.e. forms line up under first).
  577.  
  578. (put 'lambda 'lisp-indent-function 'defun)
  579. (put 'autoload 'lisp-indent-function 'defun)
  580. (put 'progn 'lisp-indent-function 0)
  581. (put 'prog1 'lisp-indent-function 1)
  582. (put 'prog2 'lisp-indent-function 2)
  583. (put 'save-excursion 'lisp-indent-function 0)
  584. (put 'save-window-excursion 'lisp-indent-function 0)
  585. (put 'save-restriction 'lisp-indent-function 0)
  586. (put 'save-match-data 'lisp-indent-function 0)
  587. (put 'let 'lisp-indent-function 1)
  588. (put 'let* 'lisp-indent-function 1)
  589. (put 'while 'lisp-indent-function 1)
  590. (put 'if 'lisp-indent-function 2)
  591. (put 'catch 'lisp-indent-function 1)
  592. (put 'condition-case 'lisp-indent-function 2)
  593. (put 'unwind-protect 'lisp-indent-function 1)
  594. (put 'with-output-to-temp-buffer 'lisp-indent-function 1)
  595.  
  596. (defun indent-sexp (&optional endpos)
  597.   "Indent each line of the list starting just after point.
  598. If optional arg ENDPOS is given, indent each line, stopping when
  599. ENDPOS is encountered."
  600.   (interactive)
  601.   (let ((indent-stack (list nil))
  602.         (next-depth 0) 
  603.         (starting-point (point))
  604.         (last-point (point))
  605.         last-depth 
  606.         bol
  607.     (outer-loop-done nil)
  608.         inner-loop-done
  609.         state
  610.         this-indent)
  611.     ;; Get error now if we don't have a complete sexp after point.
  612.     (save-excursion (forward-sexp 1))
  613.     (save-excursion
  614.       (setq outer-loop-done nil)
  615.       (while (if endpos (< (point) endpos)
  616.            (not outer-loop-done))
  617.     (setq last-depth next-depth
  618.           inner-loop-done nil)
  619.     ;; Parse this line so we can learn the state
  620.     ;; to indent the next line.
  621.     ;; This inner loop goes through only once
  622.     ;; unless a line ends inside a string.
  623.     (while (and (not inner-loop-done)
  624.             (not (setq outer-loop-done (eobp))))
  625.       (setq state (parse-partial-sexp (point) (progn (end-of-line) (point))
  626.                       nil nil state))
  627.       (setq next-depth (car state))
  628.       ;; If the line contains a comment other than the sort
  629.       ;; that is indented like code,
  630.       ;; indent it now with indent-for-comment.
  631.       ;; Comments indented like code are right already.
  632.       ;; In any case clear the in-comment flag in the state
  633.       ;; because parse-partial-sexp never sees the newlines.
  634.       (if (car (nthcdr 4 state))
  635.           (progn (indent-for-comment)
  636.              (end-of-line)
  637.              (setcar (nthcdr 4 state) nil)))
  638.       ;; If this line ends inside a string,
  639.       ;; go straight to next line, remaining within the inner loop,
  640.       ;; and turn off the \-flag.
  641.       (if (car (nthcdr 3 state))
  642.           (progn
  643.         (forward-line 1)
  644.         (setcar (nthcdr 5 state) nil))
  645.         (setq inner-loop-done t)))
  646.     (and endpos
  647.          (while (<= next-depth 0)   ;XEmacs change
  648.            (setq indent-stack (append indent-stack (list nil)))
  649.            (setq next-depth (1+ next-depth))
  650.            (setq last-depth (1+ last-depth))))
  651. ;;
  652. ;; The new method, below, sends this code into an infinite loop.  The
  653. ;; old works fine, so why bother changing it?  The new way may actually
  654. ;; be faster.  If it worked.  -- cthomp
  655. ;;
  656. ;;    (and endpos
  657. ;;         (<= next-depth 0)
  658. ;;         (progn
  659. ;;           (setq indent-stack (append indent-stack
  660. ;;                      (make-list (- next-depth) nil))
  661. ;;             last-depth (- last-depth next-depth)
  662. ;;             next-depth 0)))
  663.     (or outer-loop-done
  664.             (setq outer-loop-done (<= next-depth 0)))
  665.     (if outer-loop-done
  666.         (forward-line 1)
  667.       (while (> last-depth next-depth)
  668.         (setq indent-stack (cdr indent-stack)
  669.           last-depth (1- last-depth)))
  670.       (while (< last-depth next-depth)
  671.         (setq indent-stack (cons nil indent-stack)
  672.           last-depth (1+ last-depth)))
  673.       ;; Now go to the next line and indent it according
  674.       ;; to what we learned from parsing the previous one.
  675.       (forward-line 1)
  676.       (setq bol (point))
  677.       (skip-chars-forward " \t")
  678.       ;; But not if the line is blank, or just a comment
  679.       ;; (except for double-semi comments; indent them as usual).
  680.       (if (or (eobp) (looking-at "\\s<\\|\n"))
  681.           nil
  682.         (if (and (car indent-stack)
  683.              (>= (car indent-stack) 0))
  684.         (setq this-indent (car indent-stack))
  685.           (let ((val (calculate-lisp-indent
  686.               (if (car indent-stack) 
  687.                               (- (car indent-stack))
  688.                               starting-point))))
  689.         (if (integerp val)
  690.             (setcar indent-stack
  691.                 (setq this-indent val))
  692.           (setcar indent-stack (- (car (cdr val))))
  693.           (setq this-indent (car val)))))
  694.         (if (/= (current-column) this-indent)
  695.         (progn (delete-region bol (point))
  696.                (indent-to this-indent)))))
  697.     (or outer-loop-done
  698.         (setq outer-loop-done (= (point) last-point))
  699.         (setq last-point (point)))))))
  700.  
  701. ;; Indent every line whose first char is between START and END inclusive.
  702. (defun lisp-indent-region (start end)
  703.   (save-excursion
  704.     (goto-char start)
  705.     (and (bolp) (not (eolp))
  706.      (lisp-indent-line))
  707.     (let ((endmark (copy-marker end)))
  708.       (indent-sexp endmark)
  709.       (set-marker endmark nil))))
  710.  
  711.  
  712. ;;;; Lisp paragraph filling commands.
  713.  
  714. (defun lisp-fill-paragraph (&optional justify)
  715.   "Like \\[fill-paragraph], but handle Emacs Lisp comments.
  716. If any of the current line is a comment, fill the comment or the
  717. paragraph of it that point is in, preserving the comment's indentation
  718. and initial semicolons."
  719.   (interactive "P")
  720.   (let (
  721.     ;; Non-nil if the current line contains a comment.
  722.     has-comment
  723.  
  724.     ;; If has-comment, the appropriate fill-prefix for the comment.
  725.     comment-fill-prefix
  726.     )
  727.  
  728.     ;; Figure out what kind of comment we are looking at.
  729.     (save-excursion
  730.       (beginning-of-line)
  731.       (cond
  732.  
  733.        ;; A line with nothing but a comment on it?
  734.        ((looking-at "[ \t]*;[; \t]*")
  735.     (setq has-comment t
  736.           comment-fill-prefix (buffer-substring (match-beginning 0)
  737.                             (match-end 0))))
  738.  
  739.        ;; A line with some code, followed by a comment?  Remember that the
  740.        ;; semi which starts the comment shouldn't be part of a string or
  741.        ;; character.
  742.        ((progn
  743.       (while (not (looking-at ";\\|$"))
  744.         (skip-chars-forward "^;\n\"\\\\?")
  745.         (cond
  746.          ((eq (char-after (point)) ?\\) (forward-char 2))
  747.          ((memq (char-after (point)) '(?\" ??)) (forward-sexp 1))))
  748.       (looking-at ";+[\t ]*"))
  749.     (setq has-comment t)
  750.     (setq comment-fill-prefix
  751.           (concat (make-string (current-column) ? )
  752.               (buffer-substring (match-beginning 0) (match-end 0)))))))
  753.  
  754.     (if (not has-comment)
  755.     (fill-paragraph justify)
  756.  
  757.       ;; Narrow to include only the comment, and then fill the region.
  758.       (save-restriction
  759.     (narrow-to-region
  760.      ;; Find the first line we should include in the region to fill.
  761.      (save-excursion
  762.        (while (and (zerop (forward-line -1))
  763.                (looking-at "^[ \t]*;")))
  764.        ;; We may have gone to far.  Go forward again.
  765.        (or (looking-at "^[ \t]*;")
  766.            (forward-line 1))
  767.        (point))
  768.      ;; Find the beginning of the first line past the region to fill.
  769.      (save-excursion
  770.        (while (progn (forward-line 1)
  771.              (looking-at "^[ \t]*;")))
  772.        (point)))
  773.  
  774.     ;; Lines with only semicolons on them can be paragraph boundaries.
  775.     (let ((paragraph-start (concat paragraph-start "\\|^[ \t;]*$"))
  776.           (paragraph-separate (concat paragraph-start "\\|^[ \t;]*$"))
  777.           (fill-prefix comment-fill-prefix))
  778.       (fill-paragraph justify))))))
  779.  
  780.  
  781. (defun indent-code-rigidly (start end arg &optional nochange-regexp)
  782.   "Indent all lines of code, starting in the region, sideways by ARG columns.
  783. Does not affect lines starting inside comments or strings,
  784. assuming that the start of the region is not inside them.
  785. Called from a program, takes args START, END, COLUMNS and NOCHANGE-REGEXP.
  786. The last is a regexp which, if matched at the beginning of a line,
  787. means don't indent that line."
  788.   (interactive "r\np")
  789.   (let (state)
  790.     (save-excursion
  791.       (goto-char end)
  792.       (setq end (point-marker))
  793.       (goto-char start)
  794.       (or (bolp)
  795.       (setq state (parse-partial-sexp (point)
  796.                       (progn
  797.                         (forward-line 1) (point))
  798.                       nil nil state)))
  799.       (while (< (point) end)
  800.     (or (car (nthcdr 3 state))
  801.         (and nochange-regexp
  802.          (looking-at nochange-regexp))
  803.         ;; If line does not start in string, indent it
  804.         (let ((indent (current-indentation)))
  805.           (delete-region (point) (progn (skip-chars-forward " \t") (point)))
  806.           (or (eolp)
  807.           (indent-to (max 0 (+ indent arg)) 0))))
  808.     (setq state (parse-partial-sexp (point)
  809.                     (progn
  810.                       (forward-line 1) (point))
  811.                     nil nil state))))))
  812.  
  813. (provide 'lisp-mode)
  814.  
  815. ;;; lisp-mode.el ends here
  816.